#ifndef _SIGNATURE_H
#define _SIGNATURE_H

#include <string>
#include <map>

#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/rsa.h>
#include <openssl/engine.h>

class security_manager {
public:
    /**
     * Load the public keys stored in the given directory. The file name,
     * excluding the extension, should be the unique ID of the corresponding
     * server.
     */
    security_manager(const std::string &privkey_file,
            const std::string &passphrase, const std::string &pubkey_dir);
    ~security_manager();

    bool sign(const void *buf, unsigned buf_sz, void **sig,
            unsigned *sig_sz);

    /**
     * Verify buf using sender's public key.
     */ 
    bool verify(const void *buf, unsigned buf_sz, void *sig,
            unsigned sig_sz, const char *sender);

private:
    EVP_PKEY *privkey_;
    std::map<std::string, EVP_PKEY *> pubkeys_;

    EVP_PKEY *load_pubkey(const std::string &file);
};


#endif
